How do you get the MIME type of a file in C#?
How do you get the MIME type of a file in C#? with example.
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Anubhav Kumar
23-May-2025In C#, there are several ways to determine the MIME type of a file. Here are the most common approaches:
1. Use
Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(Recommended in ASP.NET Core)Microsoft.AspNetCore.StaticFilesNuGet package2. Use Windows Registry via
System.Web.MimeMapping(Only on Windows / ASP.NET)3. Use
Registry(Windows-only)Platform-specific — Windows only.
4. Use File Content (Magic Numbers) with
MimeDetective(Advanced)If you want to inspect the actual file content (not just the extension), use third-party libraries like:
Example using MimeDetective:
Best for file content validation (e.g., to detect forged extensions)
Summary
FileExtensionContentTypeProviderSystem.Web.MimeMappingRead More -